package com.wang.friend.elasticsearch;

import com.wang.friend.domain.question.es.QuestionES;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface QuestionRepository extends ElasticsearchRepository<QuestionES, Long> {

    //---(1)根据难度查询---
    Page<QuestionES> findQuestionByDifficulty(Integer difficulty, Pageable pageable);


    //---(2)根据关键字和难度查询---
    //select  * from tb_question where (title like '%aaa%' or content like '%bbb%')  and difficulty = 1
    @Query("""
    {
      "bool": {
        "should": [
          {
            "match_phrase": {
              "title": {
                "query": "?0",
                "slop": 0
              }
            }
          },
          {
            "match_phrase": {
              "content": {
                "query": "?1",
                "slop": 0
              }
            }
          }
        ],
        "minimum_should_match": 1,
        "must": [{"term": {"difficulty": "?2"}}]
      }
    }
    """)
    Page<QuestionES> findByTitleOrContentAndDifficulty(String keywordTitle, String keywordContent,Integer difficulty,  Pageable pageable);


    //---(3)根据关键字查询---
    //select  * from tb_question where (title like '%aaa%' or content like '%bbb%')  and difficulty = 1
    @Query("""
        {
          "bool": {
            "should": [
              {
                "match_phrase": {
                  "title": {
                    "query": "?0",
                    "slop": 0
                  }
                }
              },
              {
                "match_phrase": {
                  "content": {
                    "query": "?1",
                    "slop": 0
                  }
                }
              }
            ],
            "minimum_should_match": 1
          }
        }
        """)
    Page<QuestionES> findByTitleOrContent(String keywordTitle, String keywordContent, Pageable pageable);


}
